Ext.define('AmpleHarvest.objects.files.cFiles',
{
	extend			: 'Ext.tree.Panel',
	alias			: 'widget.ampleharvest-objects-files-cfiles',
	requires		:
	[
			'Ext.data.TreeStore',
			'AmpleHarvest.objects.files.cContextMenu',
//			'AmpleHarvest.objects.files.cToolTip',
			'AmpleHarvest.objects.files.cToolbar'
	],
	iconCls			: 'toolbar-article-navigation-files',
	title			: 'Files',
	rootVisible		: false,
	bubbleEvents	:
		[
			'doclassificationchange'
		],
	listeners		:
	{
		brokerResponse	: function(obj_response)
		{
			this.add_JsonObject(obj_response.data);
			this.ahCaller.fireEvent('objectResponse', this);
			this.fireEvent(
					AmpleHarvest.layout.cLayout.C_STR_EVENT_CLASSIFICATION_CHANGE,
					obj_response.classification
			);
		}
	},

	initComponent	: function()
	{
		this.addEvents('brokerResponse');
		
		var obj_This = this;

//		this.store = Ext.create('Ext.data.TreeStore',
//				{
//					id		: 'cFilesStore',
//					root	:
//					{
//						expanded	: true,
//						children	: []
//					},
//					fields	: ['one']
//				});
		this.store = Ext.create('Ext.data.TreeStore',
				{
					root	:
					{
						expanded	: true,
						children	: []
					}
				});
		
		this.m_obj_Toolbar = Ext
				.create('AmpleHarvest.objects.files.cToolbar');

		this.tbar = this.m_obj_Toolbar;
		
		var util_db_dmetedata = Ext.create('AmpleHarvest.util.dataBroker');
		util_db_dmetedata.brokerData(this, this.m_str_id, 'ampleharvest-objects-files-cfiles');	

		this.callParent();

		this.on("selectionchange", this.setToolbar, this);
		this.on("itemcontextmenu", function(obj_This, obj_Data, obj_HTML, int_Index, obj_Event, obj_Options)
			{
				obj_Event.stopEvent();
				this.context = Ext.create(
						'AmpleHarvest.objects.files.cContextMenu',
						{
							m_obj_Window	: obj_This
						}
				);
				this.context.set_Download(typeof obj_Data.get("download") !== "undefined");
				this.context.set_Open(typeof obj_Data.get("htmlDisplay") !== "undefined");
				this.context.set_Info(obj_Data.get("metadata").length > 0);
				this.context.showBy(obj_HTML);
			}, this
		);

		// this.getView().on('render', function(view)
		// {
		// view.tip = Ext.create(
		// 'AmpleHarvest.GUI.article.navigation.files.cToolTip',
		// {
		// target : view.el,
		// delegate : view.itemSelector,
		// listeners :
		// {
		// // Change content dynamically depending on which
		// // element triggered
		// // the show.
		// beforeshow : function updateTipBody(tip)
		// {
		// tip
		// .update(view
		// .getRecord(tip.triggerElement).data);
		// }
		// }
		// }
		// );
		// }
		// );
	},

	setToolbar		: function(obj_View, arr_Selection, obj_Options)
	{
		var obj_container = arr_Selection[0].get('container');
		
		this.m_obj_Toolbar.set_Open(typeof obj_container.htmlDisplay !== "undefined");
		this.m_obj_Toolbar.set_Download(typeof obj_container.download !== "undefined");
		this.m_obj_Toolbar.set_Info(obj_container.metadata.length > 0);
	},

	add_JsonObject	: function(JsonObject)
	{
		var win_display = '';
		this.ts_construct(JsonObject, new Array(), this.getStore()
						.getRootNode());
	},

	ts_construct	: function(JsonObject, storeArray, rootParent)
	{
		this.itemCount = 0;
		for (var value in JsonObject)
		{
			var partenNode = rootParent;
			var path = JsonObject[value].name.split('/');
			var length = path.length - 1;

			for (var i = 0; i <= length; i++)
			{
				if (i < length)
				{
					if (existNode = partenNode.findChild("text", path[i], true))
					{
						partenNode = existNode;
						continue;
					}
					partenNode = this.ts_add(partenNode, path[i], "group");
				}
				else if (i == length)
				{
					// mimeType = 'icon-'+
					// JsonObject[value].mimetype.replace('/', '-');
					//					
					// if (JsonObject[value].download)
					// {
					// var str_download_id = JsonObject[value].download.id;
					// var str_server_id = JsonObject[value].download.serverId;
					// }
					// if (JsonObject[value].htmlDisplay)
					// {
					// var str_htmlDisplay_id = JsonObject[value].htmlDisplay.id
					// var str_server_id =
					// JsonObject[value].htmlDisplay.serverId;
					// }
					// this.ts_add(partenNode, path[i], "leaf", str_server_id,
					// str_download_id, str_htmlDisplay_id, mimeType
					// );

					JsonObject[value].iconCls = 'icon-'
							+ JsonObject[value].mimetype.replace('/', '-');

					this.ts_add(partenNode, path[i], "leaf", JsonObject[value]);
				}
				this.itemCount++;
			}
		}
	},

	// ts_add : function(node_Obj, add_Obj, type, str_server_id,
	// str_download_id, str_htmlDisplay_id, icon)
	ts_add			: function(node_Obj, add_Obj, type, obj_Node)
	{
		if (type == 'group')
		{
			var appended_node = node_Obj.appendChild(
					{
						text		: add_Obj,
						expanded	: true,
						children	: []
					});
			return appended_node;
		}
		else
		{
//			obj_Node.text = add_Obj;
//			obj_Node.leaf = true;

			node_Obj.appendChild(
					{
						text			: add_Obj,
						leaf			: true,
						iconCls			: obj_Node.iconCls
					});
			node_Obj.lastChild.data.container = obj_Node;
//			console.log('test');
//			node_Obj.appendChild(obj_Node);
			
//			 node_Obj.appendChild(
//			 {
//			 text : add_Obj,
//			 iconCls : icon,
//			 serverId : str_server_id,
//			 dnloadId : str_download_id,
//			 htmlId : str_htmlDisplay_id,
//			 leaf : true
//			 });
			
			
		}
	},

	hasData			: function()
	{
		if (!this.itemCount)
		{
			return false;
		}
		if (this.itemCount < 1)
		{
			return false;
		}
		this.setTitle(this.title + ' (' + this.itemCount + ')');
		return true;
	}
}
);
